package com.kbbywyclient.presenter;

import android.content.Context;

import com.kbbywyclient.R;
import com.kbbywyclient.app.API;
import com.kbbywyclient.app.Constants;
import com.kbbywyclient.data.model.InviteShareDesModel;
import com.kbbywyclient.data.model.MinePromotionModel;
import com.kbbywyclient.presenter.base.BasePresenter;
import com.kbbywyclient.service.ApiService;
import com.kbbywyclient.service.ApiServiceHelper;
import com.kbbywyclient.ui.contarct.RecommendScoreContract;
import com.kbbywyclient.util.HttpExcUtil;
import com.kbbywyclient.util.RsaJsonUtil;
import com.kbbywyclient.util.ToastUtil;

import javax.inject.Inject;
import rx.Subscriber;

/**
 * Description:
 * Created by FHW
 * on 2017/5/9
 */

public class RecommendScorePresenter extends BasePresenter implements RecommendScoreContract.Presenter {


    @Inject
    Context mContext;
    @Inject
    ApiService apiService;
    @Inject
    RsaJsonUtil rsaJsonUtil;

    private RecommendScoreContract.View view;

    @Inject
    public RecommendScorePresenter(RecommendScoreContract.View view) {
        this.view = view;
    }

    @Override
    public void getInviteShareDes() {
        ApiServiceHelper.
                appointThreadNormal(apiService.getInviteShareDes(rsaJsonUtil.getRsaParams())).
                subscribe(new Subscriber<InviteShareDesModel>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(InviteShareDesModel model) {
                        view.closeLoadingDialog();
                        if(Constants.CODE_SUCCESS==model.getCode()){
                            view.loadInviteShareDes(model.getData().getTips());
                        }
                    }
                });
    }

    @Override
    public void getShareUrl(int salesId) {
        rsaJsonUtil.put("saleID",salesId);
        ApiServiceHelper.
                appointThreadNormal(apiService.getPromotionData(rsaJsonUtil.getRsaParams())).
                subscribe(new Subscriber<MinePromotionModel>() {
                    @Override
                    public void onCompleted() {
                        view.closeLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        view.closeLoadingDialog();
                        HttpExcUtil.printError(e);
                    }

                    @Override
                    public void onNext(MinePromotionModel model) {
                        if(model.getCode()== Constants.CODE_SUCCESS&&
                                model.getData()!=null){
                            view.loadShareUrlSuccess(API.WEB_ADDRESS+model.getData().getShareURL());
                        }else{
                            ToastUtil.showShort(model.getMsg());
                        }
                    }
                });
    }
}
